<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: File.php 2 2010-02-07 04:10:51Z indigo.sunrise $
 */

/**
 * Output class that writes to a given file.
 * 
 * @package Pwiff
 */
class Pwiff_Output_File extends Pwiff_Output
{
	/**
	 * Handle for the file.
	 * 
	 * @var resource
	 */
	protected $handle = null;
	
	/**
	 * Constructor. Opens the given file for writing.
	 * 
	 * @param  string $file Name of the file.
	 * @param  bool   $append Whether to append to the file.
	 */
	public function __construct($file, $append = false)
	{
		$this->handle = fopen($file, $append ? 'a' : 'w');
	}
	
	/**
	 * Writes a single byte to the file.
	 * 
	 * @param  int $byte
	 */
	public function writeByte($byte)
	{
		fwrite($this->handle, chr($byte), 1);
	}
	
	/**
	 * Closes the file.
	 */
	public function close()
	{
		fclose($this->handle);
	}
	
	/**
	 * Destructor. Closes the file.
	 */
	public function __destruct()
	{
		$this->close();
	}
}